package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strings"
)



func main() {

	chan1 := make(chan int, 1)
	chan2 := make(chan int, 1)
	go func() {
		chan1<-1
	}()
	go func() {
		//time.Sleep(time.Duration(200) * time.Millisecond)
		chan2<-2
	}()
	for {
		select {
		case v := <-chan1:
			fmt.Println(v)
		case v := <-chan2:
			fmt.Println(v)
		}
	}
	defer func() {
		fmt.Println("111")
	}()

	return

	getHeader := map[string]string {
		"Upgrade-Insecure-Requests": "1",
		"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52",
		"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
		"Accept-Encoding": "gzip, deflate",
		"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
		"Connection": "close",
	}
	var getResult interface{}
	go HttpGetRequest("http://xxx.xxx.xxx.xxx/solr/admin/cores?indexInfo=false&wt=json", getHeader, getResult)

	// post请求1
	postHeader1 := map[string]string {
		"Cache-Control": "max-age=0",
		"Upgrade-Insecure-Requests": "1",
		"Content-Type": "application/json",
		"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36",
		"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
		//"Referer": "http://127.0.0.1:8983/solr/tesla/config",
		"Accept-Encoding": "gzip, deflate",
		"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6",
		"Connection": "close",
	}
	var postResult1 interface{}
	go HttpGetRequest("http://xxx.xxx.xxx.xxx/solr/tesla/config", postHeader1, postResult1)

	// post请求2
	postHeader2 := map[string]string {
		"Cache-Control": "max-age=0",
		"Upgrade-Insecure-Requests": "1",
		"Content-Type": "application/x-www-form-urlencoded",
		"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36",
		"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
		//"Referer": "http://127.0.0.1:8983/solr/tesla/config",
		"Accept-Encoding": "gzip, deflate",
		"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6",
		"Connection": "close",
	}
	var postResult2 interface{}
	go HttpGetRequest("http://xxx.xxx.xxx.xxx/solr/tesla/debug/dump?param=ContentStreams", postHeader2, postResult2)
}

// http 请求
func HttpGetRequest(url string, headerArr map[string]string, result interface{}) {
	client := &http.Client{}
	//生成要访问的url
	//提交请求
	reqest, err := http.NewRequest("GET", url, nil)

	//增加header选项
	if headerArr != nil {
		for k,v := range headerArr {
			reqest.Header.Add(k, v)
		}
	}

	if err != nil {
		panic(err)
	}
	//处理返回结果
	response, _ := client.Do(reqest)
	defer response.Body.Close()
	result = response
}


// post 请求
func HttpPostRequest(url string, headerArr map[string]string, body interface{}, result interface{}) {
	client := &http.Client{}
	//生成要访问的url
	//提交请求
	bodyJson, _ := json.Marshal(body)
	reqest, err := http.NewRequest("POST", url, strings.NewReader(string(bodyJson)))

	//增加header选项
	if headerArr != nil {
		for k,v := range headerArr {
			reqest.Header.Add(k, v)
		}
	}

	if err != nil {
		panic(err)
	}
	//处理返回结果
	response, _ := client.Do(reqest)
	defer response.Body.Close()
	result = response
}
